// 订单详情页
Page({
  data: {
    order: null,
    pageLoading: true
  },

  // 页面加载
  onLoad: function(options) {
    console.log('订单详情页参数:', options);
    
    // 检查是否传入订单ID
    if (options && options.id) {
      this.orderId = options.id;
      this.getOrderDetail();
    } else {
      this.setData({
        pageLoading: false
      });
      
      wx.showToast({
        title: '未找到订单信息',
        icon: 'none'
      });
    }
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    // 刷新订单信息
    this.getOrderDetail(() => {
      wx.stopPullDownRefresh();
    });
  },
  
  // 获取订单详情
  getOrderDetail: function(callback) {
    if (!this.orderId) {
      if (callback) callback();
      return;
    }
    
    // 显示加载状态
    this.setData({
      pageLoading: true
    });
    
    const app = getApp();
    
    app.globalData.api.order.getDetail(this.orderId)
      .then(res => {
        console.log('订单详情数据:', res);
        
        // 处理订单中的图片URL
        if (res.items && res.items.length > 0) {
          res.items = res.items.map(item => {
            if (item.food_image_url) {
              // 优先使用服务器提供的完整URL
              item.food_image = item.food_image_url;
            } else if (item.food_image) {
              item.food_image = app.globalData.api.getFullImageUrl(item.food_image);
            }
            return item;
          });
        }
        
        this.setData({
          order: res,
          pageLoading: false
        });
        
        if (callback) callback();
      })
      .catch(err => {
        console.error('获取订单详情失败', err);
        
        this.setData({
          pageLoading: false
        });
        
        wx.showToast({
          title: '获取订单失败',
          icon: 'none'
        });
        
        if (callback) callback();
      });
  },
  
  // 取消订单
  cancelOrder: function() {
    wx.showModal({
      title: '取消订单',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          
          app.globalData.api.order.cancel(this.orderId)
            .then(() => {
              wx.showToast({
                title: '订单已取消',
                icon: 'success',
                duration: 2000
              });
              
              // 刷新订单信息
              setTimeout(() => {
                this.getOrderDetail();
              }, 2000);
            })
            .catch(err => {
              console.error('取消订单失败', err);
              
              wx.showToast({
                title: '取消订单失败',
                icon: 'none'
              });
            });
        }
      }
    });
  },
  
  // 支付订单（模拟）
  payOrder: function() {
    const app = getApp();
    
    app.globalData.api.order.pay(this.orderId)
      .then(() => {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        });
        
        // 刷新订单信息
        setTimeout(() => {
          this.getOrderDetail();
        }, 2000);
      })
      .catch(err => {
        console.error('支付失败', err);
        
        wx.showToast({
          title: '支付失败',
          icon: 'none'
        });
      });
  },
  
  // 确认收货
  confirmReceive: function() {
    wx.showModal({
      title: '确认收货',
      content: '确认已收到商品吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          
          app.globalData.api.order.receive(this.orderId)
            .then(() => {
              wx.showToast({
                title: '确认收货成功',
                icon: 'success',
                duration: 2000
              });
              
              // 刷新订单信息
              setTimeout(() => {
                this.getOrderDetail();
              }, 2000);
            })
            .catch(err => {
              console.error('确认收货失败', err);
              
              wx.showToast({
                title: '确认收货失败',
                icon: 'none'
              });
            });
        }
      }
    });
  },
  
  // 评价订单
  rateOrder: function() {
    wx.navigateTo({
      url: '/pages/rate/rate?id=' + this.orderId
    });
  },
  
  // 返回订单列表
  goBack: function() {
    wx.navigateBack();
  }
}); 